package cn.ffcs.uec.corp.action;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zul.Tab;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.raptornuke.portal.theme.ThemeDisplay;
import cn.ffcs.uec.corp.action.bean.UecPsnCorpMainBean;
import cn.ffcs.uec.corp.constant.UecPsnConstants;
import cn.ffcs.uec.corp.model.UecPsnCorp;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.UomZkUtil;

@Controller
@Scope("prototype")
public class UecPsnCorpMainComposer extends BasePortletComposer implements
		IPortletInfoProvider {

	private static final long serialVersionUID = -5929030282722775845L;

	private UecPsnCorpMainBean bean = new UecPsnCorpMainBean();

	private UecPsnCorp uecPsnCorp;
	
	
	@Override
	public String getPortletId() {
		return super.getPortletId();
	}

	@Override
	public ThemeDisplay getThemeDisplay() {
		return super.getThemeDisplay();
	}

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		UomZkUtil.autoFitHeight(comp);// 使用<borderlayout>时，需要加上这句进行初始化，不然从仓库调出的页面，不能显示
		Components.wireVariables(comp, bean);
		bean.getUecPsnCorpListboxExt().setPortletInfoProvider(this);
		bean.getUecPsnCorpListboxExt().addForward(
				UecPsnConstants.ON_UEC_PSN_CORP_QUERY, comp,
				UecPsnConstants.ON_UEC_OUT_STAFF_WORK_QUERY_RESPONSE);

		/*bean.getUecPsnCorpListboxExt().addForward(
				UecPsnConstants.ON_CLEAN_TAB_UEC_OUT_STAFF_INFO_QUERY, comp,
				UecPsnConstants.ON_CLEAN_TAB_UEC_OUT_STAFF_INFO_QUERY_RESPONSE);*/
		
	}

	/**
	 * 界面初始化
	 */
	public void onCreate$uecPsnCorpMainWin() {

	}

	/**
	 * 查询到外包员工基本信息的响应处理.
	 * 
	 * @param event
	 */
	public void onUecOutStaffWorkQueryResponse(final ForwardEvent event) {
		uecPsnCorp = (UecPsnCorp) event.getOrigin().getData();
		if(uecPsnCorp != null){
			callTab();
		}
	}

	public void onClickTab(ForwardEvent event) {
		try {
			Event origin = event.getOrigin();
			if (origin != null) {
				Component comp = origin.getTarget();
				if (comp != null && comp instanceof Tab) {
					bean.setTab((Tab) comp);
					callTab();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * tab响应
	 * 
	 * @throws Exception
	 * 
	 */
	public void callTab() {
		try {
			if (bean.getTab() == null) {
				bean.setTab(bean.getTabBox().getSelectedTab());
			}
			if (uecPsnCorp != null) {
				//String tab = this.bean.getTab().getId();
				/*if ("uecOutStaffWork".equals(tab)) {
					Events.postEvent(UecPsnCorpConstants.ON_UEC_OUT_STAFF_WORK_QUERY,
							this.bean.getUecOutStaffWorkListboxExt(), uecPsnCorp);
				} else if ("uecOutStaffCostB".equals(tab)) {
					uecOutStaffWork = uecOutStaffManager.findUecOutStaffWork(uecPsnCorp);
					Events.postEvent(UecPsnCorpConstants.ON_UEC_OUT_STAFF_COSTB_QUERY,
							this.bean.getUecOutStaffCostBListboxExt(), uecOutStaffWork);
				}*/
			} else {
				// 清空数据
				//bean.getUecOutStaffWorkListboxExt().onCleanUecOutWorkResponse(null);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 处理点击后Tabs .
	 * 
	 * @param event
	 */
	public void onCleanTabUecPsnCorpQueryResponse(final ForwardEvent event) {
		uecPsnCorp = (UecPsnCorp) event.getOrigin().getData();
		if(uecPsnCorp == null){
			/*Events.postEvent(UecPsnCorpConstants.ON_CLEAN_UEC_OUT_STAFF_WORK,
					bean.getUecOutStaffWorkListboxExt(), null);*/
			callTab();
		}
	}

}
